C++ Programming Multiple, Multilevel and Hierarchical Inhertiance

03-11-17 Course- CPP

C++ : Programming Multiple, Multilevel and Hierarchical Inhertiance

Levels of Inheritance

In C++ programming, a class be can derived from a derived class which is known as multilevel inhertiance. For example:


class A
{ .... ... .... };
class B : public A
{ .... ... .... };
class C : public B
{ .... ... .... };

In this example, class B is derived from class A and class C is derived from derived class B.

Example to Demonstrate the Multilevel Inheritance


#include <iostream>
using namespace std;

class A
{
    public:
      void display()
      {
          cout<<"Base class content.";
      }
};

class B : public A
{

};

class C : public B
{
 
};

int main()
{
    C c;
    c.display();
    return 0;
}

Output


Base class content.

Explanation of Program

In this program, class B is derived from A and C is derived from B. An object of class C is defined in main() function. When the display() function is called, display() in class A is executed because there is no display() function in C and B. The program first looks for display() in class C first but, can't find it. Then, looks in B because C is derived from B. Again it can't find it. And finally looks it in A and executes the codes inside that function.

If there was display() function in C too, then it would have override display() in A because of member function overriding.

Multiple Inheritance in C++

In C++ programming, a class can be derived from more than one parents. For example: A class Rectangle is derived from base classes Area and Circle.

Example of multiple inheritance in C++ programming language

Source Code to Implement Multiple Inheritance in C++ Programming

This program calculates the area and perimeter of an rectangle but, to perform this program, multiple inheritance is used.


#include <iostream>
using namespace std;
class Area
{
  public:
    float area_calc(float l,float b)
    {
        return l*b;
    }
};

class Perimeter
{
  public:
    float peri_calc(float l,float b)
    {
        return 2*(l+b);
    }
};

/* Rectangle class is derived from classes Area and Perimeter. */
class Rectangle : private Area, private Perimeter
{
    private:
        float length, breadth;
    public:
       Rectangle() : length(0.0), breadth(0.0) { }
       void get_data( )
       {
           cout<<"Enter length: ";
           cin>>length;
           cout<<"Enter breadth: ";
           cin>>breadth;
       }

       float area_calc()
       {
       /* Calls area_calc() of class Area and returns it. */

           return Area::area_calc(length,breadth);
       }

       float peri_calc()
       {
       /* Calls peri_calc() function of class Perimeter and returns it. */ 

           return Perimeter::peri_calc(length,breadth);
       }
};

int main()
{
    Rectangle r;
    r.get_data();
    cout<<"Area = "<<r.area_calc();
    cout<<"\nPerimeter = "<<r.peri_calc();
    return 0;
}

Output


Enter length: 5.1
Enter breadth: 2.3
Area = 11.73
Perimeter = 14.8

Note: This program is intended to give you idea on how multiple inheritance works rather than the condition in which multiple inheritance is used.

Ambiguity in Multiple Inheritance

Multiple inheritance may be helpful in certain cases but, sometimes odd sort of problem encounters while using multiple inheritance. For example: Two base classes have functions with same name which is not overridden in derived class and if you write code to access that function using object of derived class, compiler shows error because, it cannot determine which function to call. Here is a code for this type of ambiguity in multiple inheritance


class base1
{
  public:
     void some_function( )
     { .... ... .... }  
};
class base2
{
    void some_function( )
     { .... ... .... } 
};
class derived : public base1, public base2
{
    
};

int main()
{
    derived obj;
    
/* Error because compiler can't figure out which function to call either same_function( ) of base1 or base2 .*/    
    obj.same_function( )  
}

But, this problem can be solved easily using scope resolution function to specify which function to class either base1 or base2


int main()
{
    obj.base1::same_function( );  /* Function of class base1 is called. */
    obj.base2::same_function( );  /* Function of class base2 is called. */
}

Hierarchical Inhertiance

If more than one class is inherited from a base class, it's known as hierarchical inheritance. In general, all features that are common in child classes are included in base class in hierarchical inheritance. For example: Physics, Chemistry, Biology are derived from Science class.

Syntax of Hierarchical Inhertiance 


class base_class {
     ... .. ...
}

class first_derived_class {
     ... .. ...
}

class second_derived_class {
     ... .. ...
}

class third_derived_class {
     ... .. ...
}